package com.yxq.mdt.app.modules.spikedetail.ui;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.ToastUtils;
import com.orhanobut.hawk.Hawk;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseTabActivity;
import com.yxq.mdt.app.base.BaseTabBean;
import com.yxq.mdt.app.constant.RouteTable;
import com.yxq.mdt.app.constant.SPKey;
import com.yxq.mdt.app.modules.bottomsheet.SpikeGoodsAttrBottomSheetDialogFragment;
import com.yxq.mdt.app.modules.goodsdetail.bean.GoodsDetailBean;
import com.yxq.mdt.app.modules.market.bean.MallGoodsBean;
import com.yxq.mdt.app.modules.order.ui.OrderConfirmActivity;
import com.yxq.mdt.app.modules.personal.ui.activity.MainPersonalCustomerServiceAc;
import com.yxq.mdt.app.modules.shoppingcart.ui.CartActivity;
import com.yxq.mdt.app.modules.spikedetail.bean.GoodsCommentBean;
import com.yxq.mdt.app.modules.spikedetail.bean.SpikeGoodsDetailBean;
import com.yxq.mdt.app.modules.spikedetail.model.SpikeGoodsModel;
import com.yxq.mdt.app.modules.spikedetail.presenter.ISpikeGoodsContract;
import com.yxq.mdt.app.modules.spikedetail.presenter.SpikeGoodsPresenter;
import com.yxq.mdt.app.modules.spikeorder.ui.SpikeOrderConfirmActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author L
 * @time
 * @describe 秒杀商品详情Tab页面
 */
//@Route(path = RouteTable.ROUTER_ACTIVITY_SPIKE_GOODS_DETAIL)
public class SpikeGoodsDetailTabActivity extends BaseTabActivity<BaseTabBean, Fragment, SpikeGoodsPresenter> implements ISpikeGoodsContract.View {

    List<Fragment> fragmentList;
    SpikeGoodsDetailIndexFragment goodsDetailIndexFragment;
    SpikeGoodsDetailDetailFragment goodsDetailDetailFragment;
    SpikeGoodsDetailCommentFragment goodsDetailRecommendFragment;
    List<BaseTabBean> tabBeanList;
    @BindView(R.id.goods_detail_layout_kf)
    LinearLayout goodsDetailLayoutKf;
    @BindView(R.id.goods_detail_layout_collect)
    LinearLayout goodsDetailLayoutCollect;
    @BindView(R.id.goods_detail_layout_cart)
    LinearLayout goodsDetailLayoutCart;
    @BindView(R.id.goods_detail_tv_add_cart)
    TextView goodsDetailTvAddCart;
    @BindView(R.id.goods_detail_tv_buy)
    TextView goodsDetailTvBuy;
    @BindView(R.id.goods_detail_tab_bottom_layout)
    LinearLayout goodsDetailTabBottomLayout;
    @BindView(R.id.goods_detail_iv_collect)
    ImageView goodsDetailIvCollect;
    SpikeGoodsAttrBottomSheetDialogFragment goodsAttrBottomSheetDialogFragment;

    @Override
    public int getLayoutId() {
        return R.layout.activity_spike_goods_detail_tab;
    }

    @Override
    public List getTabList() {
        fragmentList = new ArrayList<>();
        goodsDetailIndexFragment = new SpikeGoodsDetailIndexFragment();
        goodsDetailIndexFragment.setArguments(getIntent().getExtras());
        goodsDetailDetailFragment = new SpikeGoodsDetailDetailFragment();
        goodsDetailDetailFragment.setArguments(getIntent().getExtras());
        goodsDetailRecommendFragment = new SpikeGoodsDetailCommentFragment();
        goodsDetailRecommendFragment.setArguments(getIntent().getExtras());
        fragmentList.add(goodsDetailIndexFragment);
        fragmentList.add(goodsDetailDetailFragment);
        fragmentList.add(goodsDetailRecommendFragment);
        tabBeanList = new ArrayList<>();
        tabBeanList.add(new BaseTabBean(0, "商品", 0));
        tabBeanList.add(new BaseTabBean(1, "详情", 1));
        tabBeanList.add(new BaseTabBean(2, "评价", 2));
        return tabBeanList;
    }

    @Override
    public Fragment getFragment(BaseTabBean tab) {
        Fragment fragment = fragmentList.get(tab.tabId);
        return fragment;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mViewPager.setOffscreenPageLimit(3);
        mPresenter = new SpikeGoodsPresenter(this, new SpikeGoodsModel());
        mPresenter.getGoods(getIntent().getStringExtra("id"));
        //是否是隐私模式
        boolean enable = Hawk.get(SPKey.SP_KEY_USER_LOGIN_PRIVACY, false);
        if (enable) {
            goodsDetailTvBuy.setBackgroundColor(R.drawable.shape_gray_btn_bg);
//            goodsDetailTvBuy.setEnabled(!enable);
            goodsDetailTvAddCart.setBackgroundColor(R.drawable.shape_gray_btn_bg);
//            goodsDetailTvAddCart.setEnabled(!enable);
        }
    }

    @OnClick({R.id.goods_detail_layout_kf, R.id.goods_detail_layout_collect, R.id.goods_detail_layout_cart, R.id.goods_detail_tv_add_cart, R.id.goods_detail_tv_buy})
    public void onClicked(View view) {
        switch (view.getId()) {
            case R.id.goods_detail_layout_kf://客服
                Intent intent = new Intent(mActivity, MainPersonalCustomerServiceAc.class);
                startActivity(intent);
                break;
            case R.id.goods_detail_layout_collect://收藏
                if (goodsDetailIndexFragment.getGoodsDetailBean() != null) {
                    if (goodsDetailIndexFragment.getGoodsDetailBean().getIs_favorite() == 1) {
                        mPresenter.favoriteRemove(getIntent().getStringExtra("id"));
                    } else {
                        mPresenter.favoriteAdd(getIntent().getStringExtra("id"));
                    }
                }
                break;
            case R.id.goods_detail_layout_cart://购物车
                intent = new Intent(mActivity, CartActivity.class);
                startActivity(intent);
                break;
            case R.id.goods_detail_tv_add_cart://添加购物车
                boolean enable = Hawk.get(SPKey.SP_KEY_USER_LOGIN_PRIVACY, false);
                if (enable) {
                    ToastUtils.showShort("当前隐私模式，不可以添加购物车");
                    return;
                }
                if (goodsDetailIndexFragment.getGoodsDetailBean() != null) {
                    goodsAttrBottomSheetDialogFragment = new SpikeGoodsAttrBottomSheetDialogFragment(goodsDetailIndexFragment.getGoodsDetailBean());
                    goodsAttrBottomSheetDialogFragment.setBottomSheetDialogInterface((attrBean, attr, num) -> addCart(attr, num));
                    goodsAttrBottomSheetDialogFragment.show(getSupportFragmentManager(), "GoodsAttrBottomSheetDialogFragment");
                }
                break;
            case R.id.goods_detail_tv_buy://立即购买
                enable = Hawk.get(SPKey.SP_KEY_USER_LOGIN_PRIVACY, false);
                if (enable) {
                    ToastUtils.showShort("当前隐私模式，不可以购买");
                    return;
                }
                //判断是否有多个规格，多个规格需要弹出框选择规格
                if (goodsDetailIndexFragment.getGoodsDetailBean() != null) {
//                    if (goodsDetailIndexFragment.getGoodsDetailBean().getAttr().size() > 1) {
                    goodsAttrBottomSheetDialogFragment = new SpikeGoodsAttrBottomSheetDialogFragment(goodsDetailIndexFragment.getGoodsDetailBean());
                    goodsAttrBottomSheetDialogFragment.setBottomSheetDialogInterface(new SpikeGoodsAttrBottomSheetDialogFragment.BottomSheetDialogInterface() {
                        @Override
                        public void onclick(GoodsDetailBean.AttrBean attrBean, String attr, int num) {
                            //构造规格
                            SpikeGoodsDetailBean data = JSON.parseObject(JSON.toJSONString(goodsDetailIndexFragment.getGoodsDetailBean()), SpikeGoodsDetailBean.class);
                            List<GoodsDetailBean.AttrBean> attrBeanList = data.getAttr();
                            attrBeanList.set(0, attrBean);
                            data.setAttr(attrBeanList);
                            data.setNum(num);
                            data.setId(data.getMiaosha().getMiaosha_goods_id());//设置ID
                            Intent intent = new Intent(mActivity, SpikeOrderConfirmActivity.class);
                            intent.putExtra("from", "goods");
                            intent.putExtra("data", JSON.toJSONString(data));
                            startActivity(intent);
                        }
                    });
                    goodsAttrBottomSheetDialogFragment.show(getSupportFragmentManager(), "GoodsAttrBottomSheetDialogFragment");
//                        return;
//                    }
//                    intent = new Intent(mActivity, SpikeOrderConfirmActivity.class);
//                    intent.putExtra("from", "goods");
//                    SpikeGoodsDetailBean data = goodsDetailIndexFragment.getGoodsDetailBean();
//                    data.setId(data.getMiaosha().getMiaosha_goods_id());//设置ID
//                    intent.putExtra("data", JSON.toJSONString(data));
//                    startActivity(intent);
                }
                break;
        }
    }

    /**
     * 添加购物车
     *
     * @param attr 规格
     * @param num  数量
     */
    private void addCart(String attr, int num) {
        mPresenter.addCart(getIntent().getStringExtra("id"), num, attr);
    }

    @Override
    public void onLoadSuccess(SpikeGoodsDetailBean goodsDetailBean) {
        if (goodsDetailBean != null) {
            goodsDetailIvCollect.setImageResource(goodsDetailBean.getIs_favorite() == 1 ? R.mipmap.ic_collect_checked : R.mipmap.ic_collect);
        }
    }

    @Override
    public void onLoadFailed(String err) {

    }

    @Override
    public void onLoadRecommendSuccess(MallGoodsBean mallGoodsBean) {

    }

    @Override
    public void onLoadRecommendFailed(String err) {

    }

    @Override
    public void onAddSuccess(String err) {
        ToastUtils.showShort("收藏成功");
        if (goodsDetailIndexFragment.getGoodsDetailBean() != null) {
            goodsDetailIndexFragment.getGoodsDetailBean().setIs_favorite(1);
            goodsDetailIvCollect.setImageResource(R.mipmap.ic_collect_checked);
        }
    }

    @Override
    public void onAddFailed(String err) {
        ToastUtils.showShort(err);
    }

    @Override
    public void onRemoveSuccess(String err) {
        ToastUtils.showShort("取消收藏成功");
        if (goodsDetailIndexFragment.getGoodsDetailBean() != null) {
            goodsDetailIndexFragment.getGoodsDetailBean().setIs_favorite(0);
            goodsDetailIvCollect.setImageResource(R.mipmap.ic_collect);
        }
    }

    @Override
    public void onRemoveFailed(String err) {
        ToastUtils.showShort(err);
    }

    @Override
    public void onLoadCommentSuccess(GoodsCommentBean goodsCommentBean) {

    }

    @Override
    public void onLoadCommentFailed(String err) {

    }

    @Override
    public void onAddCartSuccess(String result) {
        ToastUtils.showShort(result);
    }

    @Override
    public void onAddCartFailed(String err) {
        ToastUtils.showShort(err);
    }

    /**
     * 设置选中Tab
     *
     * @param tab
     */
    public void setCurrentTab(int tab) {
        if (tab >= mViewPager.getAdapter().getCount()) {
            return;
        }
        mViewPager.setCurrentItem(tab);
    }
}
